package com.atguigu.day10;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

public class Flink11_SQL_Time {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);


        // 1. 创建表的执行环境
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        Configuration configuration = tableEnv.getConfig().getConfiguration();
        configuration.setString("table.local-time-zone", "GMT");

        // 创建表, 声明一个额外的列作为处理时间
//        tableEnv.executeSql("create table sensor(id string,ts bigint,vc int,pt as PROCTIME()) with("
//                + "'connector' = 'filesystem',"
//                + "'path' = 'input/sensor-sql.txt',"
//                + "'format' = 'csv'"
//                + ")");
//

        //创建表，指定事件时间字段
        tableEnv.executeSql("create table sensor(" +
                "id string," +
                "ts bigint," +
                "vc int, " +
//                "t as to_timestamp(from_unixtime(ts/1000,'yyyy-MM-dd HH:mm:ss'))," +
                "t as to_timestamp_ltz(ts,3)," +
                "watermark for t as t - interval '5' second)" +
                "with("
                + "'connector' = 'filesystem',"
                + "'path' = 'input/sensor-sql.txt',"
                + "'format' = 'csv'"
                + ")"
        );

        tableEnv.executeSql("Select * from sensor").print();


    }
}
